package cn.gok.client.service.impl;

import cn.gok.client.service.GoodsService;
import cn.gok.common.dao.goods.GoodsAdversDao;
import cn.gok.common.dao.goods.GoodsCategoryMapper;
import cn.gok.common.entity.goods.GoodsAdvers;
import cn.gok.common.entity.goods.GoodsCategory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author shikundai
 * @date 2020/12/9
 * @time 16:33
 * @description
 */
@Service("clientGoodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsAdversDao goodsAdversDao;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategory> getGoodsBrandList() {
        /*
         * 获得一级标签
         * */
        //构造实体
        GoodsCategory goodsCategory = new GoodsCategory();
        //设置父类ID为-1，代表一级标题
        goodsCategory.setParentId("-1");
        //构造查询条件
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(goodsCategory);
        List<GoodsCategory> parents = goodsCategoryMapper.selectList(queryWrapper);
        /*
        * 循环查询子类
        * */
        for (GoodsCategory g:parents) {
            g=getSons(g);
        }
        return parents;
    }

    @Override
    public GoodsCategory getSons(GoodsCategory parent) {
        /*
         * 根据父类ID获取商品集合
         * */
        List<GoodsCategory> list = goodsCategoryMapper.listByParentId(parent.getCategoryId().toString());
        parent.setSon(list);
        /*
        *  还有子类
        * 开始递归调用
        * */
        if (list.size()>0){
            for (GoodsCategory g:list) {
                g=getSons(g);
            }
        }
        return parent;
    }

    @Override
    public List<GoodsAdvers> getGoodsAdvers() {
        /*
        * 构造条件
        * （1）广告还没结束
        * （2）广告已经开始
        * （3）需要展示的
        * */
        QueryWrapper<GoodsAdvers> wrapper=new QueryWrapper<>();
        Date date=new Date();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString=df.format(date);
        //广告还没结束
        wrapper.ge("END_TIME",dateString);
        //广告已经开始
        wrapper.le("START_TIME",dateString);
        //需要展示
        wrapper.eq("IS_SHOW","1");
        List<GoodsAdvers> list=goodsAdversDao.selectList(wrapper);
        return list;
    }
}

